using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using ActivateYourGlutes.Infrastructure;
using ActivateYourGlutes.Model;
using ActivateYourGlutes.Model.Contracts;
using ActivateYourGlutes.Model.Providers;
using ActivateYourGlutes.Payloads.MeasurementTypePayloads;

namespace ActivateYourGlutes.Controllers
{
    public partial class MeasurementTypeController : LocalisingController
    {
        private readonly IMeasurementTypeProvider _measurementTypeProvider;

        public MeasurementTypeController(IMeasurementTypeProvider measurementTypeProvider)
        {
            _measurementTypeProvider = measurementTypeProvider;
        }

        [Authorize(Roles = "User")]
        public virtual ActionResult Index()
        {
            ViewData.Model = _measurementTypeProvider.MeasurementTypes(_measurementTypeProvider.CurrentAccount(User));
            return View();
        }

        [Authorize(Roles = "User")]
        public virtual ActionResult Create()
        {
            var payload = new EditCreatePayload()
                                 {
                                     MeasurementType = new MeasurementType(),
                                     ValueTypes = ValueTypes()
                                 };
            payload.ValueTypes.First().Selected = true;
            ViewData.Model = payload;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles="User")]
        public virtual ActionResult Create(MeasurementType measurementType)
        {
            if (!ModelState.IsValid)
            {
                ViewData.Model = new EditCreatePayload()
                {
                    MeasurementType = measurementType,
                    ValueTypes = ValueTypes()
                };
                return View();
            }
            var currentAccount = _measurementTypeProvider.CurrentAccount(User);
            try
            {
                _measurementTypeProvider.AddToAccount(currentAccount, measurementType);
                return RedirectToAction(MVC.MeasurementType.Index());
            }
            catch (ValidationException ex)
            {
                ViewData.Model = new EditCreatePayload()
                {
                    MeasurementType = measurementType,
                    ValueTypes = ValueTypes()
                };
                ex.AddToModelState(ViewData.ModelState);
                return View();
            }
        }

        private IEnumerable<SelectListItem> ValueTypes()
        {
            return
                _measurementTypeProvider.ValueTypes().Select(
                    vt => new SelectListItem() {Selected = false, Text = vt.Description, Value = vt.Code});
        }

        [Authorize(Roles="User")]
        public virtual ActionResult ConfirmDelete(int id)
        {
            var currentAccount = _measurementTypeProvider.CurrentAccount(User);
            try
            {
                ViewData.Model = _measurementTypeProvider.GetById(currentAccount, id);
            }
            catch (AuthorizationException)
            {
                return RedirectToAction(MVC.Account.Unauthorized());
            }
            return View();
        }

        [Authorize(Roles = "User")]
        public virtual ActionResult Delete(int id, string response)
        {
            if (response.Equals("Yes"))
            {
                var currentAccount = _measurementTypeProvider.CurrentAccount(User);
                try
                {
                    _measurementTypeProvider.Delete(currentAccount, id);
                }
                catch (AuthorizationException)
                {
                    return RedirectToAction(MVC.Account.Unauthorized());
                }
            }
            return RedirectToAction(MVC.MeasurementType.Index());
        }
    }
}
